﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.lia.utils {
    import flash.events.*;
    import flash.display.*;
    import flash.utils.*;
    import flash.system.*;
    import flash.net.*;
    import flash.ui.*;

    public class SWFProfiler {

        private static var itvTime:int;
        private static var initTime:int;
        private static var currentTime:int;
        private static var frameCount:int;
        private static var totalCount:int;
        public static var minFps:Number;
        public static var maxFps:Number;
        public static var minMem:Number;
        public static var maxMem:Number;
        public static var history:int = 60;
        public static var refreshRate:Number = 1;
        public static var fpsList:Array = [];
        public static var memList:Array = [];
        private static var displayed:Boolean = false;
        private static var started:Boolean = false;
        private static var inited:Boolean = false;
        private static var frame:Sprite;
        private static var stage:Stage;
        private static var content:ProfilerContent;
        private static var ci:ContextMenuItem;

        public static function init(_arg1:Stage, _arg2:InteractiveObject):void{
            if (inited){
                return;
            };
            inited = true;
            stage = _arg1;
            content = new ProfilerContent();
            frame = new Sprite();
            minFps = Number.MAX_VALUE;
            maxFps = Number.MIN_VALUE;
            minMem = Number.MAX_VALUE;
            maxMem = Number.MIN_VALUE;
            var _local3:ContextMenu = new ContextMenu();
            _local3.hideBuiltInItems();
            ci = new ContextMenuItem("Show Profiler", true);
            _local3.customItems = [ci];
            _arg2.contextMenu = _local3;
            addEvent(ci, ContextMenuEvent.MENU_ITEM_SELECT, onSelect);
            start();
        }
        public static function start():void{
            if (started){
                return;
            };
            started = true;
            initTime = (itvTime = getTimer());
            totalCount = (frameCount = 0);
            addEvent(frame, Event.ENTER_FRAME, draw);
        }
        public static function stop():void{
            if (!started){
                return;
            };
            started = false;
            removeEvent(frame, Event.ENTER_FRAME, draw);
        }
        public static function gc():void{
            try {
                new LocalConnection().connect("foo");
                new LocalConnection().connect("foo");
            } catch(e:Error) {
            };
        }
        public static function get currentFps():Number{
            return ((frameCount / intervalTime));
        }
        public static function get currentMem():Number{
            return (((System.totalMemory / 0x0400) / 0x0400));
        }
        public static function get averageFps():Number{
            return ((totalCount / runningTime));
        }
        private static function get runningTime():Number{
            return (((currentTime - initTime) / 1000));
        }
        private static function get intervalTime():Number{
            return (((currentTime - itvTime) / 1000));
        }
        private static function onSelect(_arg1:ContextMenuEvent):void{
            if (!displayed){
                show();
            } else {
                hide();
            };
        }
        public static function show():void{
            ci.caption = "Hide Profiler";
            displayed = true;
            stage.addChild(content);
            updateDisplay();
        }
        public static function hide():void{
            ci.caption = "Show Profiler";
            displayed = false;
            stage.removeChild(content);
        }
        private static function draw(_arg1:Event=null):void{
            currentTime = getTimer();
            frameCount++;
            totalCount++;
            if (intervalTime >= refreshRate){
                if (displayed){
                    updateDisplay();
                } else {
                    updateMinMax();
                };
                fpsList.unshift(currentFps);
                memList.unshift(currentMem);
                if (fpsList.length > history){
                    fpsList.pop();
                };
                if (memList.length > history){
                    memList.pop();
                };
                itvTime = currentTime;
                frameCount = 0;
            };
        }
        private static function updateDisplay():void{
            updateMinMax();
            content.update(runningTime, minFps, maxFps, minMem, maxMem, currentFps, currentMem, averageFps, fpsList, memList, history);
        }
        private static function updateMinMax():void{
            if (!(currentFps > 0)){
                return;
            };
            minFps = Math.min(currentFps, minFps);
            maxFps = Math.max(currentFps, maxFps);
            minMem = Math.min(currentMem, minMem);
            maxMem = Math.max(currentMem, maxMem);
        }
        private static function addEvent(_arg1:EventDispatcher, _arg2:String, _arg3:Function):void{
            _arg1.addEventListener(_arg2, _arg3, false, 0, true);
        }
        private static function removeEvent(_arg1:EventDispatcher, _arg2:String, _arg3:Function):void{
            _arg1.removeEventListener(_arg2, _arg3);
        }

    }
}//package com.lia.utils 

import flash.events.*;
import flash.display.*;
import flash.text.*;

class ProfilerContent extends Sprite {

    private var minFpsTxtBx:TextField;
    private var maxFpsTxtBx:TextField;
    private var minMemTxtBx:TextField;
    private var maxMemTxtBx:TextField;
    private var infoTxtBx:TextField;
    private var box:Shape;
    private var fps:Shape;
    private var mb:Shape;

    public function ProfilerContent():void{
        this.fps = new Shape();
        this.mb = new Shape();
        this.box = new Shape();
        this.mouseChildren = false;
        this.mouseEnabled = false;
        this.fps.x = 65;
        this.fps.y = 45;
        this.mb.x = 65;
        this.mb.y = 90;
        var _local1:TextFormat = new TextFormat("_sans", 9, 0xAAAAAA);
        this.infoTxtBx = new TextField();
        this.infoTxtBx.autoSize = TextFieldAutoSize.LEFT;
        this.infoTxtBx.defaultTextFormat = new TextFormat("_sans", 11, 0xCCCCCC);
        this.infoTxtBx.y = 98;
        this.minFpsTxtBx = new TextField();
        this.minFpsTxtBx.autoSize = TextFieldAutoSize.LEFT;
        this.minFpsTxtBx.defaultTextFormat = _local1;
        this.minFpsTxtBx.x = 7;
        this.minFpsTxtBx.y = 37;
        this.maxFpsTxtBx = new TextField();
        this.maxFpsTxtBx.autoSize = TextFieldAutoSize.LEFT;
        this.maxFpsTxtBx.defaultTextFormat = _local1;
        this.maxFpsTxtBx.x = 7;
        this.maxFpsTxtBx.y = 5;
        this.minMemTxtBx = new TextField();
        this.minMemTxtBx.autoSize = TextFieldAutoSize.LEFT;
        this.minMemTxtBx.defaultTextFormat = _local1;
        this.minMemTxtBx.x = 7;
        this.minMemTxtBx.y = 83;
        this.maxMemTxtBx = new TextField();
        this.maxMemTxtBx.autoSize = TextFieldAutoSize.LEFT;
        this.maxMemTxtBx.defaultTextFormat = _local1;
        this.maxMemTxtBx.x = 7;
        this.maxMemTxtBx.y = 50;
        addChild(this.box);
        addChild(this.infoTxtBx);
        addChild(this.minFpsTxtBx);
        addChild(this.maxFpsTxtBx);
        addChild(this.minMemTxtBx);
        addChild(this.maxMemTxtBx);
        addChild(this.fps);
        addChild(this.mb);
        this.addEventListener(Event.ADDED_TO_STAGE, this.added, false, 0, true);
        this.addEventListener(Event.REMOVED_FROM_STAGE, this.removed, false, 0, true);
    }
    public function update(_arg1:Number, _arg2:Number, _arg3:Number, _arg4:Number, _arg5:Number, _arg6:Number, _arg7:Number, _arg8:Number, _arg9:Array, _arg10:Array, _arg11:int):void{
        var _local19:Number;
        if (_arg1 >= 1){
            this.minFpsTxtBx.text = (_arg2.toFixed(3) + " Fps");
            this.maxFpsTxtBx.text = (_arg3.toFixed(3) + " Fps");
            this.minMemTxtBx.text = (_arg4.toFixed(3) + " Mb");
            this.maxMemTxtBx.text = (_arg5.toFixed(3) + " Mb");
        };
        this.infoTxtBx.text = (((((("Current Fps " + _arg6.toFixed(3)) + "   |   Average Fps ") + _arg8.toFixed(3)) + "   |   Memory Used ") + _arg7.toFixed(3)) + " Mb");
        this.infoTxtBx.x = ((stage.stageWidth - this.infoTxtBx.width) - 20);
        var _local12:Graphics = this.fps.graphics;
        _local12.clear();
        _local12.lineStyle(1, 0x33FF00, 0.7);
        var _local13:int;
        var _local14:int = _arg9.length;
        var _local15 = 35;
        var _local16:int = (stage.stageWidth - 80);
        var _local17:Number = (_local16 / (_arg11 - 1));
        var _local18:Number = (_arg3 - _arg2);
        _local13 = 0;
        while (_local13 < _local14) {
            _local19 = ((_arg9[_local13] - _arg2) / _local18);
            if (_local13 == 0){
                _local12.moveTo(0, (-(_local19) * _local15));
            } else {
                _local12.lineTo((_local13 * _local17), (-(_local19) * _local15));
            };
            _local13++;
        };
        _local12 = this.mb.graphics;
        _local12.clear();
        _local12.lineStyle(1, 26367, 0.7);
        _local13 = 0;
        _local14 = _arg10.length;
        _local18 = (_arg5 - _arg4);
        _local13 = 0;
        while (_local13 < _local14) {
            _local19 = ((_arg10[_local13] - _arg4) / _local18);
            if (_local13 == 0){
                _local12.moveTo(0, (-(_local19) * _local15));
            } else {
                _local12.lineTo((_local13 * _local17), (-(_local19) * _local15));
            };
            _local13++;
        };
    }
    private function added(_arg1:Event):void{
        this.resize();
        stage.addEventListener(Event.RESIZE, this.resize, false, 0, true);
    }
    private function removed(_arg1:Event):void{
        stage.removeEventListener(Event.RESIZE, this.resize);
    }
    private function resize(_arg1:Event=null):void{
        var _local2:Graphics = this.box.graphics;
        _local2.clear();
        _local2.beginFill(0, 0.9);
        _local2.drawRect(0, 0, stage.stageWidth, 120);
        _local2.lineStyle(1, 0xFFFFFF, 0.2);
        _local2.moveTo(65, 45);
        _local2.lineTo(65, 10);
        _local2.moveTo(65, 45);
        _local2.lineTo((stage.stageWidth - 15), 45);
        _local2.moveTo(65, 90);
        _local2.lineTo(65, 55);
        _local2.moveTo(65, 90);
        _local2.lineTo((stage.stageWidth - 15), 90);
        _local2.endFill();
        this.infoTxtBx.x = ((stage.stageWidth - this.infoTxtBx.width) - 20);
    }

}
